/*
 * Filename:    AuditToWarningExtractServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年12月29日
 */
package com.paic.mhis.hcpms.warning.biz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.paic.mhis.hcpms.warning.biz.service.AuditToWarningExtractService;
import com.paic.mhis.hcpms.warning.biz.service.WarningWorkFlowService;
import com.paic.mhis.hcpms.warning.dao.AuditToWarningDAO;
import com.paic.mhis.hcpms.warning.dto.HcpmsWarnTargetDTO;
import com.paic.mhis.hcpms.warning.dto.HcpmsWarningDTO;
import com.paic.mhis.hcpms.warning.intergration.dao.HcpmsWarnDAO;
import com.paic.mhis.hcpms.warning.util.WarnConstants;

/**
 * 稽核转预警抽取数据实现类
 * 
 * @author EX-LILIUZHU001
 * 
 */

@Service("auditToWarningExtractService")
public class AuditToWarningExtractServiceImpl implements AuditToWarningExtractService {

	@Resource(name = "auditToWarningDao")
	private AuditToWarningDAO auditToWarningDao;
	@Autowired
	private HcpmsWarnDAO hcpmsWarnDao;
	@Autowired
	private WarningWorkFlowService warningWorkFlowService;

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void extractAuditData() throws Exception {
		try {
			// 抽取事件数据
			List<HcpmsWarningDTO> auditToWarningData = auditToWarningDao.queryAuditToWarningData();
			for (HcpmsWarningDTO warning : auditToWarningData) {
				String otherCaseCode = warning.getOtherCaseCode();
				Map<String, String> paramsMap = new HashMap<String, String>();
				paramsMap.put("problemCode", otherCaseCode);
				// 根据caseCode查询已经插入的条数
				int caseCount = auditToWarningDao.queryAuditToWarningCount(paramsMap);
				if (caseCount > 0) {
					continue;
				}
				String warnNo = hcpmsWarnDao.createWarnNo(WarnConstants.BUSINESS_WARING);
				warning.setWarnNo(warnNo);
				warning.setIsValid(WarnConstants.IS_VALID_0);
				warning.setSource(WarnConstants.WARN_SOURCE_08);
				warning.setSourceType("02");
				warning.setSourceObject(WarnConstants.AUDIT_TO_WARNING_SOURCE_OBJECT);
				// 导出事件信息
				hcpmsWarnDao.createHcpmsWarn(warning);
				List<HcpmsWarnTargetDTO> targetData = auditToWarningDao.queryAuditToWarningTargetData(otherCaseCode);
				for (HcpmsWarnTargetDTO target : targetData) {
					target.setWarnNo(warnNo);
					target.setIsValid(WarnConstants.IS_VALID_0);
					// 导出事件对象信息
					hcpmsWarnDao.createHcpmsWarnTarget(target);
				}
				try {
					// 启动
					Map<String, String> condition = new HashMap<String, String>();
					condition.put("SOURCE", WarnConstants.WARN_SOURCE_08);
					String actCurrent = warningWorkFlowService.startWorkFlow(warnNo, null, condition);
					// 更新状态
					HcpmsWarningDTO warnDto = new HcpmsWarningDTO();
					warnDto.setWarnNo(warnNo);
					warnDto.setStatus(actCurrent);
					hcpmsWarnDao.updateHcpmsWarn(warnDto);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			// 数据状态变更为已处理
			auditToWarningDao.updateAuditToWarningData();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
